package com.xpec.c4.game.time.listener;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间监听器管理器<br>
 * 
 * @author suyinglong
 * @version 2012-3-14 下午04:46:49
 */
public class TimeListenerManager {
	
	/**
	 * 记录时间修改监听器<br>
	 * 
	 * @author suyinglong
	 * @version 2012-3-14 下午04:44:24
	 */
	private List<TimeChangeListener> tcListeners = new ArrayList<TimeChangeListener>();

	/**
	 * 记录时间修改监听器的名称<br>
	 * 
	 * @author suyinglong
	 * @version 2012-3-14 下午04:45:04
	 */
	private List<String> tcListenersNames = new ArrayList<String>();

	private static TimeListenerManager instance;

	private TimeListenerManager() {
		super();
	}

	public static TimeListenerManager getInstance() {
		if (instance == null) {
			instance = new TimeListenerManager();
		}
		return instance;
	}

	/**
	 * 增加时间变化监听器<br>
	 * 
	 * @param listener
	 *            监听器对象
	 * 
	 * @author suyinglong
	 * @version 2012-3-14 下午04:47:11
	 */
	public void addTimeChangeListener(TimeChangeListener listener) {
		if(listener == null ) return;
		String className = listener.getClass().getName();
		if(listener.getTagID() != null && listener.getTagID().length() > 0)
			className = className + listener.getTagID();
		if (!tcListenersNames.contains(className)) {
			tcListeners.add(listener);
			tcListenersNames.add(className);
		}
	}

	/**
	 * 时间变化时的处理<br>
	 * 
	 * @author suyinglong
	 * @version 2012-3-14 下午04:49:19
	 */
	public void processTimeChanged() {
		for (TimeChangeListener listener : tcListeners) {
			listener.excute();
		}
	}
}
